package com.liulei.test;

import java.util.Arrays;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: liulei
 * Date: 14-1-16
 * Time: 上午11:23
 * To change this template use File | Settings | File Templates.
 */
public class PermutationAndCombination {
    private static boolean f[];
    public static void main(String[] args) {
        int[] arr = new int[]{1,2,3,4,5};
        for(int i=0;i<arr.length;i++){
            int sum=arr[i];
            f=new boolean[sum];
            Arrays.fill(f, true);
            int[] num=new int[sum];
            for(int j=0;j<sum;j++){
                num[j]=j+1;
            }
            int nn=arr.length;
            String str="";
            count(num,str,nn);
        }
    }
    /**
     *
     * @param num 表示要排列的数组
     * @param str 以排列好的字符串
     * @param nn  剩下需要排列的个数，如果需要全排列，则nn为数组长度
     */
    private static void count(int[] num, String str, int nn) {
        if(nn==0){
            System.out.println(str);
            return;
        }
        for(int i=0;i<num.length;i++){
            if(!f[i]){
                continue;
            }
            f[i]=false;
            count(num,str+num[i],nn-1);
            f[i]=true;
        }
    }
}
